var _tokens = require('./token1.js')
const { exec } = require('child_process');
//----------这是一条分割线-------//
var step = 50;
var j = 0;

var tokens = []
_tokens = _tokens.replace(/^\s+|\s+|[\r\n]$/g, "")
tokens = _tokens.split(',')

var request = require('request')
var indexUrl = 'https://www.bearead.com/mobile/index.html'
var voteUrl = 'https://www.bearead.com/api/subscribe/vote/vote'

function parseCookie(cookie) {
    var json = {};
    var arr = cookie.split(';')
    arr.forEach(function(el) {
        var key = el.split('=')[0];
        var value = el.split('=')[1];
        json[key] = value;
    }, this);
    return json
}

function vote(token, i) {
    var options = {
        method: 'POST',
        url: 'https://www.bearead.com/api/subscribe/vote/vote',
        headers: {
            'cache-control': 'no-cache',
            'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Cookie': 'UM_distinctid=15d50ebd262afc-08a7fef3fef6fa-574e6e46-3d10d-15d50ebd263188; CNZZDATA1259041947=1185091838-1500297497-https%253A%252F%252Fwww.bearead.com%252F%7C1500297497; Hm_lvt_c466ced4c1ec8e05233af71563715f7b=1500132893,1500213688,1500298283; Hm_lpvt_c466ced4c1ec8e05233af71563715f7b=1500301503;token=' + token + ';'
        },
        form: { cpid: 'cp1007367', cvlid: '11' },
        pool: { maxSockets: Infinity }

    };

    request(options, function(error, response, body) {
        if (error) {
            console.log('服务端错误' + i);
            // exec('pm2 stop bx_vote1.js', function(error, stdout, stderr) {
            //     if (error) { console.error('error: ' + error); }
            //     console.log('stdout: ' + stdout);
            //     console.log('stderr: ' + stderr);
            // });
        }
        try {
            if (JSON.parse(body).message == 'success') { console.log('(' + token + ')-' + '投票' + '-' + j + '-' + i + '-' + 'times') } else {
                console.log('(' + token + ')-' + '失败' + '-' + j + '-' + i + '-' + 'times')
            }
        } catch (err) {
            console.log(token + "(请求错误)" + i)
        }
    });


}

function task(token) {
    return new Promise((resolve, reject) => {
        var i = 1
        vote(token, i);
        var voting = setInterval(function() {
            i++
            vote(token, i);
            if (i == 105) {
                clearInterval(voting);
                resolve()
            }

        }, (Math.random() * 10000 + 30000));
    })
}

function go() {
    var len = tokens.length;
    if (len < step) {
        var promise_list = []
        tokens.forEach(function(el) {
            promise_list.push(task(el))
        }, this);
        Promise.all(promise_list).then(function(result) {
            console.log('全部完成');
            setTimeout(function() {
                exec('pm2 stop bx_vote1.js', function(error, stdout, stderr) {
                    if (error) { console.error('error: ' + error); }
                    console.log('stdout: ' + stdout);
                    console.log('stderr: ' + stderr);
                });
            }, 2000)
        }).catch(function(err) { console.log(err) })
    } else {
        var promise_list = []
        tokens.slice(0, step).forEach(function(el) {
            promise_list.push(task(el))
        }, this);
        Promise.all(promise_list).then(function(result) {
            j++;
            console.log('完成' + j + '组(' + step * j + '个)');
            tokens.splice(0, step)
            go();
        }).catch(function(err) { console.log(err) })
    }
}
go();

//power by ruriai_cn